<?php

namespace app\admin\services;

use app\admin\controller\Admin;
use app\admin\model\AdminModel;
use app\admin\model\ModuleModel;
use app\admin\model\RoleModel;
use think\facade\Validate;
use think\facade\Request;

class PersonalCenterService extends BaseService
{
    public static function index($id){
        $adminInfo=AdminModel::field('role,id,nickname,avatar,phone,last_login_time,account')->find($id);
        $role=RoleModel::find($adminInfo['role']);
        $moduleList=ModuleModel::where([
            ['id','in',explode('-',$role['permission'])]
        ])->field('module_name')->select();
        $adminInfo->last_login_time=date('Y-m-d H:i:s',$adminInfo->last_login_time);
        $adminInfo->role=$role['role_name'];
        $adminInfo->avatar = Request::scheme().'://'.Request::host().'/static/upload/'.$adminInfo->avatar;
        $return=[
            'moduleList'=>$moduleList,
            'adminInfo'=>$adminInfo
        ];
        api_return(200,'',$return);
    }
    public static function edit($p){
        $rule=[
            'nickname'=>'require',
            'phone'=>'require',
            'id'=>'require',
        ];
        $validate=Validate::rule($rule);
        if(!$validate->check($p))
            api_return(50001,$validate->getError());
        $res=AdminModel::editAdmin($p);
        api_return($res['code'],$res['msg']);
    }
    public static function updatePwd($p){
        $rule=[
            'id'=>'require',
            'old'=>'require',
            'new'=>'require',
            'checkNew'=>'require|confirm:new',
        ];
        $validate=Validate::rule($rule);
        if(!$validate->check($p))
            api_return(50001,$validate->getError());
        AdminModel::updatePwd($p['id'],$p['old'],$p['new']);

    }
}
